package com.visionblue.business.ship.print

import com.visionblue.business.ship.modbus.utilities.ByteUtil
import com.visionblue.business.ship.model.bean.local.PrintInfo

/**
 * @author: xlh
 * @date: 2020-08-03 10:49 AM
 * @desc:
 */
class E9LTicketPrintFormat : PrintFormat {

    private val mBytes = mutableListOf<Byte>()

    override fun toBytes(printInfo: PrintInfo): ByteArray {
        var yPoint: Int = Y_START
        val tag = listOf<Byte>(0x1B, 0x40, 0x1A, 0x5B, 0x00)

        mBytes.addAll(tag)
        val textList = mutableListOf("被接收方：${printInfo.fromShip}")
        textList.addAll(printInfo.wasteInfoMap)
        textList.add("接收方：${printInfo.toShip}")
        textList.add("日期：${printInfo.date}")

        //污染物信息
        textList.forEach {
            val value = it
            yPoint += Y_DELTA
            writeTextFromLeft(yPoint, value)
        }

        //
        yPoint += (Y_DELTA * 2)
        writeTextFromLeft(yPoint, "被接收方签字：")

        yPoint += (Y_DELTA * 2)
        writeTextFromLeft(yPoint, "接收方签字：")

        if (!printInfo.pdfUrl.isNullOrEmpty()) {
            yPoint += (Y_DELTA * 2)
            writeQrCode(yPoint, printInfo.pdfUrl)
            yPoint += (Y_DELTA * 5)
            val fromInt16 = ByteUtil.fromInt16(yPoint)
            writeTextInternal(listOf(0x50.toByte(), 0x00, fromInt16[1], fromInt16[0]), "扫描可查看电子联单")
        }

        writeEnd()
        return mBytes.toByteArray()
    }

    private fun writeQrCode(yPoint: Int, qrCode: String) {
        val start = listOf<Byte>(0x1A, 0x31, 0x00, 0x05, 0x03)
        val yResults = ByteUtil.fromInt16(yPoint)
        val startPoint = listOf(0x70.toByte(), 0x00, yResults[1], yResults[0])
        val codeSize = listOf<Byte>(0x04)
        val rotate = listOf<Byte>(0x00)

        mBytes.addAll(start)
        mBytes.addAll(startPoint)
        mBytes.addAll(codeSize)
        mBytes.addAll(rotate)
        val toTypedArray = qrCode.toByteArray().toList()
        mBytes.addAll(toTypedArray)
        mBytes.add(DIRECTIVE_END)
    }

    private fun writeTextFromLeft(yPoint: Int, value: String) {
        val fromInt16 = ByteUtil.fromInt16(yPoint)
        writeTextInternal(listOf(0x00, 0x00, fromInt16[1], fromInt16[0]), value)
    }

    private fun writeTextInternal(startPoint: List<Byte>, content: String) {
        val start = listOf<Byte>(0x1A, 0x54, 0x01)
        val textSize = listOf(0x18, 0x00.toByte(), 0x00, 0x11)
        mBytes.addAll(start)
        mBytes.addAll(startPoint)
        mBytes.addAll(textSize)
        if (content.length > 20) {
            mBytes.addAll(("${content.substring(0, 20)}...").toGBKHex())
        } else {
            mBytes.addAll(content.toGBKHex())
        }
        mBytes.add(DIRECTIVE_END)
    }

    private fun writeEnd() {
        mBytes.addAll(listOf(0x1A, 0x5D, 0x00, 0x1A, 0x4F, 0x00))
    }

    companion object {
        private const val DIRECTIVE_END = 0x00.toByte()
        private const val Y_DELTA = 32
        private const val Y_START = 0x15
    }

}